package com.Home
{
	import com.*;
	import com.Engine.*;
	import com.Home.*;
	import com.Packet.*;
	import com.UI.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.external.*;
	import flash.filters.*;
	import flash.geom.*;
	import flash.net.*;
	import flash.system.*;
	import flash.text.*;
	import flash.ui.*;
	import flash.utils.*;
	
	import gs.*;
	import gs.easing.*;
	import gs.events.TweenEvent;
	
	import org.aswing.*;
	import org.aswing.border.*;
	import org.aswing.border.EmptyBorder;
	import org.aswing.colorchooser.*;
	import org.aswing.ext.*;
	import org.aswing.geom.*;
	import org.aswing.plaf.*;
	import org.aswing.skinbuilder.SkinProgressBarBackground;
	import org.aswing.skinbuilder.SkinProgressBarForeground;
	
	public class Home
	{
		public function Home()
		{
		}
		public static function isInHome():Boolean
		{
			return CharInfo.mapType == Const.MAP_TYPE_HOME;
		}
		public static function isInSelfHome():Boolean
		{
			return CharInfo.mapType == Const.MAP_TYPE_HOME && CharInfo.mapOwnerId == CharInfo.charId;//Home._objHomeInfoDest.m_nCharId == CharInfo.charId;
		}

		static private var _objHomeInfoDest:Object = [];
		static private var _objHomeInfoSelf:Object = [];
		public static function isInvade():Boolean
		{
			
			return homeInfo.nDestInvade != null && homeInfo.nDestInvade == 1;
		}
		public static function phHomeEnter_1300(e:PacketEvent):void
		{
			var msg:Array = e.data as Array;
			trace("phHomeEnter_1300", msg);
			var nIndex:uint = 0;
			var m_nCharId:uint = msg[nIndex++];
			if(homeInfo.m_nCharId == null || homeInfo.m_nCharId != m_nCharId)
				homeInfo.m_arBuildings = [];
			if(homeInfo.m_nCharId == m_nCharId)
			{
				if(SceneMain.getInstance().buildAllObject)
					updateHomeBuildings();
			}
			homeInfo.m_nCharId = m_nCharId;
			homeInfo.nDestInvade = msg[nIndex++];
			homeInfo.m_szCharName = msg[nIndex++];
			homeInfo.m_nRace = msg[nIndex++];
			homeInfo.m_nGender = msg[nIndex++];
			homeInfo.m_nLevel = msg[nIndex++];
			PanelMainHelp.getInstance().updateMapInfo();
		}
		
		public static function get homeInfo():Object
		{
			if(_objHomeInfoDest.m_arBuildings == null)
				_objHomeInfoDest.m_arBuildings = [];
			if(_objHomeInfoSelf.m_arBuildings == null)
				_objHomeInfoSelf.m_arBuildings = [];
			
			return (isInHome() && !isInSelfHome()) ? _objHomeInfoDest : _objHomeInfoSelf;
		}
		
		public static function get buildings():Array
		{
			return homeInfo.m_arBuildings;
		}
		public static var _timeTaskUpdate:Timer = null;
		public static function phHomeBuildings_1301(e:PacketEvent):void
		{
			if(homeInfo.m_nCharId == null)
				homeInfo.m_nCharId = CharInfo.charId;
			var msg:Array = e.data as Array;
			trace("phHomeBuildings_1301", msg);
			var nIndex:uint = 0;
			var m_nCharId:uint = msg[nIndex++];
			if(m_nCharId != homeInfo.m_nCharId)
				return;
			homeInfo.m_nCharId = m_nCharId;
			var nCount:uint = msg[nIndex++];
			var array:Array = buildings;
			//array.splice(0,array.length);
			for(var i:int = 0; i < nCount; i++)
			{
				var obj:Object = new Object();

				obj.m_nCSVId = msg[nIndex++];
				obj.m_nInnerId = msg[nIndex++];
				obj.m_nState = msg[nIndex++];
				//array[obj.m_nInnerId-1] = obj;
				var bFind:Boolean = false;
				for(var k:int = 0; k < array.length; k++)
				{
					if(array[k].m_nInnerId != null && int(array[k].m_nInnerId) == int(obj.m_nInnerId))
					{
						array[k] = obj;
						bFind = true;
						break;
					}
				}
				if(!bFind)
					array.push(obj);
				obj.m_nLevel = msg[nIndex++];
				if(obj.m_nLevel == 0)
					continue;
				obj.m_nGainTime = msg[nIndex++];
				obj.m_nGainStartTime = msg[nIndex++];
				obj.m_nLevelupTime = msg[nIndex++];
				obj.m_nLevelupStartTime = msg[nIndex++];
				obj.m_nLevelupReqairWood = msg[nIndex++];
				obj.m_nLevelupReqairFood = msg[nIndex++];

				obj.m_nGuardType = msg[nIndex++];
				obj.m_nGuardInnerId = msg[nIndex++];
				
				if(obj.m_nGuardType == PanelMain.PARTNER_TYPE_PET)
				{
					
					obj.m_nPetId = msg[nIndex++];
					if(obj.m_nPetId != 0)
					{	
						obj.m_nPetName = msg[nIndex++];
						obj.m_nPetImageId = msg[nIndex++];
					}
				}
				
				var j:int = 0;
				if(obj.m_nGuardType == PanelMain.PARTNER_TYPE_LOVER)
				{
					obj.m_nLoverId = msg[nIndex++];
					if(obj.m_nLoverId != 0)
					{
						obj.m_nLoverCharId = msg[nIndex++];
						obj.m_nLoverCharName = msg[nIndex++];
						obj.m_nLoverRace = msg[nIndex++];
						obj.m_nLoverGender = msg[nIndex++];
						obj.m_arLoverEquips = new Array();
						obj.m_nLoverEquips = msg[nIndex++];
						for(j = 0; j < obj.m_nLoverEquips; j++)
						{
							obj.m_arLoverEquips[j] =  msg[nIndex++];
						}
					}
				}
				
				if(obj.m_nGuardType == PanelMain.PARTNER_TYPE_SLAVE)
				{
					obj.m_nSlaveId = msg[nIndex++];
					if(obj.m_nSlaveId != 0)
					{
						obj.m_nSlaveCharId = msg[nIndex++];
						obj.m_nSlaveCharName = msg[nIndex++];
						obj.m_nSlaveRace = msg[nIndex++];
						obj.m_nSlaveGender = msg[nIndex++];
						obj.m_arSlaveEquips = new Array();
						obj.m_nSlaveEquips = msg[nIndex++];
						for(j = 0; j < obj.m_nSlaveEquips; j++)
						{
							obj.m_arSlaveEquips[j] =  msg[nIndex++];
						}
					}
				}
				
				obj.m_nCaptureCharId = msg[nIndex++];
				obj.m_szCaptureCharName = msg[nIndex++];
				obj.m_nCaptureRace = msg[nIndex++];
				obj.m_nCaptureGender = msg[nIndex++];
				obj.m_nCaptureLevel = msg[nIndex++];
				obj.m_nCaptureTime = msg[nIndex++];
			}
			
			Home.sort();
			if(SceneMain.getInstance().buildAllObject)
				updateHomeBuildings();
			if(_timeTaskUpdate == null)
			{
				_timeTaskUpdate = new Timer(1000);
				_timeTaskUpdate.addEventListener(TimerEvent.TIMER,onTimerUpdateHomeBuildingsInfo);
			}
			if(m_nCharId == CharInfo.charId)
			{
				if(!_timeTaskUpdate.running)
					_timeTaskUpdate.start();
			}
			else
			{
				if(_timeTaskUpdate.running)
					_timeTaskUpdate.stop();
			}
			if(FrameHomeMain.isShowing())
				FrameHomeMain._frame.update();
		}
		public static var m_arBuyableEggs:Array = [];
		public static var m_arBuyableBooks:Array = [];
		public static function phHomePetBuyableItems_1309(e:PacketEvent):void
		{
			if(homeInfo.m_nCharId == null)
				homeInfo.m_nCharId = CharInfo.charId;
			var msg:Array = e.data as Array;
			trace("phHomePetBuyableItems_1309", msg);
			var nIndex:uint = 0;
			var i:int = 0;
			var obj:Object = null;
			var m_nCharId:uint = msg[nIndex++];
			var nCount:uint = msg[nIndex++];
			var array:Array = m_arBuyableEggs;

			for(i = 0; i < nCount; i++)
			{
				obj = new Object();
				obj.index = i;
				obj.m_nItemId = msg[nIndex++];
				obj.m_nPrice = msg[nIndex++];
				obj.m_tRefreshTime = msg[nIndex++];

				array[i] = obj;
			}
			
			nCount = msg[nIndex++];
			array = m_arBuyableBooks;
			for(i = 0; i < nCount; i++)
			{
				obj = new Object();
				obj.index = i;
				obj.m_nItemId = msg[nIndex++];
				obj.m_nPrice = msg[nIndex++];
				obj.m_tRefreshTime = msg[nIndex++];
				
				array[i] = obj;
			}
			FrameHomeBuilding10Pet.update();
		}
		public static function sort():void
		{
			var array:Array = buildings;
			for each(var obj:Object in array)
			{

				obj.weight = int(1);
				if(obj.m_nState == Const.HOME_BUILDING_STATE_CAPTURE)
				{
					obj.weight = int(4);
				}

				//协助
				if(obj.m_nState == Const.HOME_BUILDING_STATE_LEVELUP)
				{
					obj.weight = int(3);
				}
				
				//收获
				if(obj.m_nState == Const.HOME_BUILDING_STATE_NORMAL)
				{
					if(obj.m_nGainStartTime >= obj.m_nGainTime)
						obj.weight = int(2);
				}
					
				
			}
			
			//array.sortOn("m_nLevel",Array.DESCENDING);
			array.sortOn("weight",Array.DESCENDING|Array.NUMERIC);

		}
		public static function onClickBuildingHome(obj:ObjectHomeBuilding):void
		{
			if(!isInSelfHome() &&!isInvade() && obj.panelAssist != null)
			{
				Client.getInstance().sendPacketLogic(1307,[int(Home.homeInfo.m_nCharId),int(obj.buildingHomeId)]);
				return;
			}
			if(isInSelfHome() &&!isInvade() && obj.panelGain != null)
			{
				Client.getInstance().sendPacketLogic(1308,[int(Home.homeInfo.m_nCharId),int(obj.buildingHomeId)]);
				return;
			}
			if(isInvade())
				FrameHomeInvade.show(obj);
			else
			{
				if(obj.buildingHomeType == 0)
					FrameHomePlant.show(obj);
				if(obj.buildingHomeType == 1)
					FrameHomeBuilding1Slave.show(obj);
				if(obj.buildingHomeType == 2)
					FrameHomeBuilding2Cellar.show(obj);
				if(obj.buildingHomeType == 3)
					FrameHomeBuilding3Repository.show(obj);
				if(obj.buildingHomeType == 4)
					FrameHomeBuilding4Market.show(obj);
				if(obj.buildingHomeType == 5)
					FrameHomeBuilding5Cate.show(obj);
				if(obj.buildingHomeType == 6)
					FrameHomeBuilding6Tech.show(obj);
				if(obj.buildingHomeType == 7)
					FrameHomeBuilding7Main.show(obj);
				if(obj.buildingHomeType == 8)
					FrameHomeBuilding8Tree.show(obj);
				if(obj.buildingHomeType == 9)
					FrameHomeBuilding9Equip.show(obj);
				if(obj.buildingHomeType == 10)
					FrameHomeBuilding10Pet.show(obj);
				if(obj.buildingHomeType == 11)
					FrameHomeBuilding11Land.show(obj);
				if(obj.buildingHomeType == 12)
					FrameHomeBuilding12Greedy.show(obj);
				if(obj.buildingHomeType == 13)
					FrameHomeBuilding13DoorGod.show(obj);
				if(obj.buildingHomeType == 14)
					FrameHomeBuilding14Warlord.show(obj);
				if(obj.buildingHomeType == 15)
					FrameHomeBuilding15Wood.show(obj);
				if(obj.buildingHomeType == 16)
					FrameHomeBuilding16Food.show(obj);
			}
		}
		
		public static function onUpdateBuildingHome(obj:ObjectHomeBuilding):void
		{

				if(obj.buildingHomeType == 1 && FrameHomeBuilding1Slave.isShowing() && FrameHomeBuilding1Slave._frame.objDest == obj)
					FrameHomeBuilding1Slave.show(obj);
				if(obj.buildingHomeType == 2 && FrameHomeBuilding2Cellar.isShowing() && FrameHomeBuilding2Cellar._frame.objDest == obj)
					FrameHomeBuilding2Cellar.show(obj);
				if(obj.buildingHomeType == 3 && FrameHomeBuilding3Repository.isShowing() && FrameHomeBuilding3Repository._frame.objDest == obj)
					FrameHomeBuilding3Repository.show(obj);
				if(obj.buildingHomeType == 4 && FrameHomeBuilding4Market.isShowing() && FrameHomeBuilding4Market._frame.objDest == obj)
					FrameHomeBuilding4Market.show(obj);
				if(obj.buildingHomeType == 5 && FrameHomeBuilding5Cate.isShowing() && FrameHomeBuilding5Cate._frame.objDest == obj)
					FrameHomeBuilding5Cate.show(obj);
				if(obj.buildingHomeType == 6 && FrameHomeBuilding6Tech.isShowing() && FrameHomeBuilding6Tech._frame.objDest == obj)
					FrameHomeBuilding6Tech.show(obj);
				if(obj.buildingHomeType == 7 && FrameHomeBuilding7Main.isShowing() && FrameHomeBuilding7Main._frame.objDest == obj)
					FrameHomeBuilding7Main.show(obj);
				if(obj.buildingHomeType == 8 && FrameHomeBuilding8Tree.isShowing() && FrameHomeBuilding8Tree._frame.objDest == obj)
					FrameHomeBuilding8Tree.show(obj);
				if(obj.buildingHomeType == 9 && FrameHomeBuilding9Equip.isShowing() && FrameHomeBuilding9Equip._frame.objDest == obj)
					FrameHomeBuilding9Equip.show(obj);
				if(obj.buildingHomeType == 10 && FrameHomeBuilding10Pet.isShowing() && FrameHomeBuilding10Pet._frame.objDest == obj)
					FrameHomeBuilding10Pet.show(obj);
				if(obj.buildingHomeType == 11 && FrameHomeBuilding11Land.isShowing() && FrameHomeBuilding11Land._frame.objDest == obj)
					FrameHomeBuilding11Land.show(obj);
				if(obj.buildingHomeType == 12 && FrameHomeBuilding12Greedy.isShowing() && FrameHomeBuilding12Greedy._frame.objDest == obj)
					FrameHomeBuilding12Greedy.show(obj);
				if(obj.buildingHomeType == 13 && FrameHomeBuilding13DoorGod.isShowing() && FrameHomeBuilding13DoorGod._frame.objDest == obj)
					FrameHomeBuilding13DoorGod.show(obj);
				if(obj.buildingHomeType == 14 && FrameHomeBuilding14Warlord.isShowing() && FrameHomeBuilding14Warlord._frame.objDest == obj)
					FrameHomeBuilding14Warlord.show(obj);
				if(obj.buildingHomeType == 15 && FrameHomeBuilding15Wood.isShowing() && FrameHomeBuilding15Wood._frame.objDest == obj)
					FrameHomeBuilding15Wood.show(obj);
				if(obj.buildingHomeType == 16 && FrameHomeBuilding16Food.isShowing() && FrameHomeBuilding16Food._frame.objDest == obj)
					FrameHomeBuilding16Food.show(obj);
		}
		
		public static function visitHome(destCharId:int,invade:Boolean = false):void
		{
			Client.getInstance().sendPacketLogic(1300,[destCharId,invade?int(1):int(0)]);
		}
		
		public static function updateHomeBuildings():void
		{
			Home.sort();
			var array:Array = buildings;
			for each(var obj:Object in array)
			{
				var building:ObjectHomeBuilding = SceneMain.getInstance().getObjectByBuildingHomeId(obj.m_nInnerId);
				if(building != null)
				{
					building.setLevel(obj.m_nLevel);
					var objCSV:Object = Client.getInstance().GetHomeBuildingCSV(building.buildingHomeType);
					if(objCSV != null)
					{
						building.setName(objCSV.name);
					}
					building.drawName();
					building.userObject = obj;
					building.enableMouseOn = (isInSelfHome() || isInvade());
					
					//被占领状态
					if(building.panelCapture == null)
					{
						var pane:PanelHomeCapture = new PanelHomeCapture();
						pane.setBackgroundDecorator(null);
						pane.x = (building.getImageWidth() - pane.width) / 2 - building.getImageWidth()/2;
						pane.y = -building.getImageHeight()/2-pane.height-20;
						building.addChild(pane);
						building.panelCapture = pane;
					}
					if(building.userObject.m_nState == Const.HOME_BUILDING_STATE_CAPTURE)
					{
						var mOn:Boolean = building.mouseOn;
						building.mouseOn = false;
						building._renderBitmap.filters = new Array(new GlowFilter(0xEA0000, 0.6, building.getImageWidth(), building.getImageHeight(), 2,1,true));//new Array(new GlowFilter(0xff0000, 1, 20, 20, 2));
						building.mouseOn = mOn;
					}
					else
						building._renderBitmap.filters = null;
					(building.panelCapture as PanelHomeCapture).item = building;
					//----------------
					var img_background:DisplayObject = null;
					//收获
					if(isInSelfHome() &&!isInvade() && building.userObject.m_nState == Const.HOME_BUILDING_STATE_NORMAL && obj.m_nGainStartTime >= obj.m_nGainTime)
					{
						if(building.panelGain == null)
						{
							building.panelGain = new PanelHomeAssist();
							img_background = Client.getUIElement("img_home_gain") as DisplayObject
							building.panelGain.setBackgroundDecorator(new AssetBackground(img_background));
							building.panelGain.setSize(new IntDimension(img_background.width, img_background.height));
							building.panelGain.setPreferredSize(new IntDimension(img_background.width, img_background.height));
							building.panelGain.setClipMasked(false);
							building.panelGain.x = (building.getImageWidth() - building.panelGain.width) / 2 - building.getImageWidth()/2;
							building.panelGain.y = -building.getImageHeight()-building.panelGain.height;
							building.panelGain.setToolTipText("点击收获");
							building.addChild(building.panelGain);
						}
						building.enableMouseOn = true;
					}
					else if (building.panelGain != null)
					{
						building.removeChild(building.panelGain);
						building.panelGain = null;
					}
					
					//----------------

					//协助
					if(!isInSelfHome() &&!isInvade() && ((building.userObject.m_nState == Const.HOME_BUILDING_STATE_LEVELUP&& obj.m_nLevelupStartTime < obj.m_nLevelupTime) 
						|| (building.userObject.m_nState == Const.HOME_BUILDING_STATE_NORMAL&& obj.m_nGainStartTime < obj.m_nGainTime)))
					{
						if(building.panelAssist == null)
						{
							building.panelAssist = new PanelHomeAssist();
							img_background = Client.getUIElement("img_home_assist") as DisplayObject
							building.panelAssist.setBackgroundDecorator(new AssetBackground(img_background));
							building.panelAssist.setSize(new IntDimension(img_background.width, img_background.height));
							building.panelAssist.setPreferredSize(new IntDimension(img_background.width, img_background.height));
							building.panelAssist.setClipMasked(false);
							building.panelAssist.x = (building.getImageWidth() - building.panelAssist.width) / 2 - building.getImageWidth()/2;
							building.panelAssist.y = -building.getImageHeight()-building.panelAssist.height;
							building.panelAssist.setToolTipText("点击协助");
							building.addChild(building.panelAssist);
						}
						building.enableMouseOn = true;
					}
					else if (building.panelAssist != null)
					{
						building.removeChild(building.panelAssist);
						building.panelAssist = null;
					}
					
					onUpdateBuildingHome(building);
				}
			}
		}
		public static function onTimerUpdateHomeBuildingsInfo(e:TimerEvent):void
		{
			var needUpdate:Boolean = false;
			var needUpdateState:Boolean = false;
			var array:Array = buildings;
			for each(var obj:Object in array)
			{
				var building:ObjectHomeBuilding = null;
				if(obj.m_nState == Const.HOME_BUILDING_STATE_LEVELUP && obj.m_nLevelupTime > obj.m_nLevelupStartTime)
				{
					obj.m_nLevelupStartTime++;
					if( obj.m_nLevelupStartTime >= obj.m_nLevelupTime)
					{
						Client.getInstance().sendPacketLogic(1302,[int(obj.m_nInnerId),int(obj.m_nCSVId),int(0)]);
						needUpdateState = true;
					}
					building = SceneMain.getInstance().getObjectByBuildingHomeId(obj.m_nInnerId);
					if(building != null)
					{
						onUpdateBuildingHome(building);
					}
					needUpdate = true;
				}
				
				if(obj.m_nState == Const.HOME_BUILDING_STATE_NORMAL && obj.m_nGainTime > obj.m_nGainStartTime)
				{
					obj.m_nGainStartTime++;
					if( obj.m_nGainStartTime >= obj.m_nGainTime)
					{
						needUpdateState = true;
						//Client.getInstance().sendPacketLogic(1308,[int(Home._objDestHomeInfo.m_nCharId),int(obj.m_nInnerId)]);
					}
					building = SceneMain.getInstance().getObjectByBuildingHomeId(obj.m_nInnerId);
					if(building != null)
					{
						onUpdateBuildingHome(building);
					}
					needUpdate = true;
				}
			}
			if(needUpdate && FrameHomeMain.isShowing())
				FrameHomeMain._frame.update();
			if(needUpdateState)
			{
				updateHomeBuildings();
			}
		}
	}
}